package springtest.pdf;

import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.itextpdf.text.pdf.BaseFont;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * @author chenzhiqiang
 * @date 2018-08-01 15:14:47
 */
@Controller
public class PdfController {
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @RequestMapping("/html")
    public String getHtml(Model mod) {
        Map<String, Object> o = new HashMap<String, Object>();
        o.put("name", "cc");
        mod.addAllAttributes(o);
        return "tpl";
    }

    @RequestMapping("/show/html")
    public void showHtml(HttpServletResponse response) throws Exception {
        String pdfName = "pdfName.html";// pdf名字
        response.setHeader("Content-disposition", "inline;filename=" + pdfName);// 响应头为在线查看
        response.setContentType("text/html; charset=utf-8");
        OutputStream os = response.getOutputStream();
        FileInputStream fis = new FileInputStream(
                "C:/Users/Admin/Documents/zee-home/report/reportResult/html/change/change_2018-09-05_10-29-31.html");
        int count = 0;
        byte[] buffer = new byte[1024 * 1024];
        while ((count = fis.read(buffer)) != -1)
            os.write(buffer, 0, count);
        os.flush();
        if (os != null)
            os.close();
        if (fis != null)
            fis.close();
    }

    @RequestMapping("/pdf")
    public void getPdf(HttpServletResponse response) throws Exception {
        String pdfName = "pdfName.pdf";// pdf名字
        response.setHeader("Content-disposition", "inline;filename=" + pdfName);// 响应头为在线查看
        response.setContentType("application/pdf");
        OutputStream os = response.getOutputStream();
        ITextRenderer renderer = new ITextRenderer();
        // 生成模板文件
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Template template = configuration.getTemplate("tpl.ftl");
        Map<Object, Object> o = new HashMap<Object, Object>();
        o.put("name", "cc");
        StringWriter stringWriter = new StringWriter();
        template.process(o, stringWriter);
        // 生成的模板文件转成pdf
        renderer.setDocumentFromString(stringWriter.toString());
        renderer.getSharedContext().setBaseURL("file:/E:/zee/spring-boot/src/main/java/springtest/pdf/resources/");
        renderer.getFontResolver().addFont(
                "file:/E:/zee/spring-boot/src/main/java/springtest/pdf/resources/ARIALUNI.TTF", BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED);
        renderer.layout();
        renderer.createPDF(os);
        os.close();
    }

    // public static void main(String[] args) throws Exception {
    // Configuration configuration = new
    // Configuration(Configuration.getVersion());
    // String string = PdfController.class.getResource("/").getFile();
    // configuration.setDirectoryForTemplateLoading(new File(string +
    // "templates/ftl"));
    // configuration.setDefaultEncoding("utf-8");
    // Template template = configuration.getTemplate("test.html");
    // FileWriter writer = new FileWriter(new File("E:/00m2/test/test1.html"));
    //
    // MongoTemplate mongo = new MongoTemplate(new MongoClient("127.0.0.1",
    // 27017), "zeerule");
    // Map<Object, Object> o = new HashMap<Object, Object>();
    // Criteria criteria = new Criteria();
    // Query query = new Query(criteria);
    // List<Object> netWork = mongo.find(query, Object.class,
    // "NETWORK_COMPARE_RESULT");
    // o.put("ipGroupList", netWork);
    // List<Object> service = mongo.find(query, Object.class,
    // "SERVICE_COMPARE_RESULT");
    // o.put("serviceList", service);
    // List<FilterRuleDto> rule = mongo.find(query, FilterRuleDto.class,
    // "POLICY_COMPARE_RESULT");
    // o.put("ruleList", rule);
    // List<DeviceInterfaceDto> interfaces = mongo.find(query,
    // DeviceInterfaceDto.class,
    // "DEVICEINTERFACE_COMPARE_RESULT");
    // o.put("interfaceList", interfaces);
    // List<InterfaceGroupDto> zone = mongo.find(query, InterfaceGroupDto.class,
    // "ZONE_COMPARE_RESULT");
    // o.put("interfaceGroupList", zone);
    // List<RoutingEntryDto> routing = mongo.find(query, RoutingEntryDto.class,
    // "ROUTING_COMPARE_RESULT");
    // o.put("routeList", routing);
    // template.process(o, writer);
    // writer.close();
    // // return stringWriter.toString();
    // }
    //
    // @Test
    // public void testa() {
    // }
    //
    // public static void a() throws Exception {
    // Configuration configuration = new
    // Configuration(Configuration.getVersion());
    // // configuration
    // // .setDirectoryForTemplateLoading(new
    // // File("E:/zee/spring-boot/src/main/java/springtest/pdf/resources"));
    // String string = PdfController.class.getResource("/").getFile();
    // configuration.setDirectoryForTemplateLoading(new File(string +
    // "templates/ftl"));
    // configuration.setDefaultEncoding("utf-8");
    //
    // IPItemDto ipItemDto = new IPItemDto();
    // ipItemDto.setName("ipitem");
    // ipItemDto.setDeviceChangeType(DeviceChangeType.DELETED);
    // ipItemDto.setDescription("sdas");
    // ipItemDto.setDescription_premodified("asd");
    // ipItemDto.setType(IPItemType.HOST_IP);
    // ipItemDto.setIp4Prefix(IP4Address.create("192.168.101.123"));
    // List<IPItemDto> list = new ArrayList<>();
    // list.add(ipItemDto);
    // List<String> iplist = new ArrayList<>();
    //
    // // IpAddressesChange a = new IpAddressesChange();
    // // a.setDeviceChangeType(DeviceChangeType.ADDED);
    // // List<String> alist = new ArrayList<>();
    // // alist.add("192.168.1.1");
    // // alist.add("192.168.3");
    // // a.setIpIpAddresses(alist);
    // // IpAddressesChange b = new IpAddressesChange();
    // // b.setDeviceChangeType(DeviceChangeType.NO_CHANGED);
    // // List<String> blist = new ArrayList<>();
    // // blist.add("192.168.1.1/23");
    // // b.setIpIpAddresses(blist);
    // // List<IpAddressesChange> list4 = new ArrayList<>();
    // // list4.add(b);
    // // list4.add(a);
    // // ipItemDto.setIpAddressesList(list4);
    //
    // IPGroupDto ipGroupDto = new IPGroupDto();
    // ipGroupDto.setName("IPGroupDto");
    // List<IPGroupDto> list2 = new ArrayList<>();
    // ipGroupDto.setDeviceChangeType(DeviceChangeType.UPDATED);
    // ipGroupDto.setDescription("s");
    // ipGroupDto.setDescription_premodified("b");
    // List<IPItemDto> list5 = new ArrayList<>();
    // list5.add(ipItemDto);
    // ipGroupDto.setIncludeItemsDto(list5);
    // list2.add(ipGroupDto);
    // List<StringChange> list6 = new ArrayList<>();
    // StringChange stringChange = new StringChange();
    // stringChange.setDeviceChangeType(DeviceChangeType.ADDED);
    // stringChange.setValue("aGroup");
    // list6.add(stringChange);
    // StringChange stringChange1 = new StringChange();
    // stringChange1.setDeviceChangeType(DeviceChangeType.DELETED);
    // stringChange1.setValue("dGroup");
    // list6.add(stringChange1);
    // ipGroupDto.setIncludeGroupNamesDto(list6);
    // ipGroupDto.setExcludeGroupNamesDto(list6);
    //
    // // 服务
    // FilterServiceDto dto = new FilterServiceDto();
    // dto.setDeviceChangeType(DeviceChangeType.ADDED);
    // dto.setName("a");
    // dto.setName_premodified("b");
    // dto.setDescription("s");
    // dto.setDescription("d");
    // dto.setType(FilterServiceType.SERVICE_TCP_UDP);
    // List<FilterServiceDto> list3 = new ArrayList<>();
    // list3.add(dto);
    //
    // // 接口
    // DeviceInterfaceDto interfaceDto = new DeviceInterfaceDto();
    // interfaceDto.setDeviceChangeType(DeviceChangeType.DELETED);
    // interfaceDto.setName("a");
    // interfaceDto.setShutdown(true);
    // interfaceDto.setShutdown_premodified(false);
    // // interfaceDto.setName_premodified("b");
    // interfaceDto.setSecurityLevel_premodified(22);
    // interfaceDto.setInterfaceGroupName_premodified("cc");
    // interfaceDto.setDescription_premodified("ceshi");
    // IPAddressInfoDto ipAddressInfoDto = new IPAddressInfoDto();
    // ipAddressInfoDto.setDeviceChangeType(DeviceChangeType.ADDED);
    // ipAddressInfoDto.setIp4Address(IP4Address.create("192.168.1.6"));
    // ipAddressInfoDto.setIp4MaskLength(24);
    // IPAddressInfoDto ipAddressInfoDto1 = new IPAddressInfoDto();
    // ipAddressInfoDto1.setDeviceChangeType(DeviceChangeType.DELETED);
    // ipAddressInfoDto1.setIp4Address(IP4Address.create("192.168.1.6"));
    // ipAddressInfoDto1.setIp4MaskLength(24);
    // List<DeviceInterfaceDto> list7 = new ArrayList<>();
    // List<IPAddressInfoDto> arrayList = new ArrayList<>();
    // arrayList.add(ipAddressInfoDto);
    // arrayList.add(ipAddressInfoDto1);
    // interfaceDto.setIpAddressesDto(arrayList);
    // list7.add(interfaceDto);
    //
    // // zone
    // InterfaceGroupDto groupDto = new InterfaceGroupDto();
    // groupDto.setDeviceChangeType(DeviceChangeType.DELETED);
    // groupDto.setName("a");
    // groupDto.setName_premodified("a");
    // List<InterfaceGroupDto> list8 = new ArrayList<>();
    // List<DeviceInterfaceDto> interfaceNames2 = new ArrayList<>();
    // interfaceNames2.add(interfaceDto);
    // groupDto.setInterfaceNames2(list7);
    // groupDto.setDescription("aaa");
    // groupDto.setDescription_premodified("vv");
    // list8.add(groupDto);
    //
    // FilterRuleDto ruleDto = new FilterRuleDto();
    // ruleDto.setDeviceChangeType(DeviceChangeType.ADDED);
    // ruleDto.setName("a");
    // ruleDto.setDeviceType("aaaa");
    // ruleDto.setRuleId("a");
    // StringChange stringChanged = new StringChange();
    // stringChanged.setDeviceChangeType(DeviceChangeType.ADDED);
    // stringChanged.setValue("aGroup");
    // List<StringChange> list9 = new ArrayList<>();
    // list9.add(stringChanged);
    // // ruleDto.setInInterfaceGroupRefs(list9.toArray(new
    // // StringChange[list9.size()]));
    // List<FilterRuleDto> list10 = new ArrayList<>();
    // list10.add(ruleDto);
    //
    // Template template = configuration.getTemplate("test.html");
    // FileWriter writer = new FileWriter(new File("E:/00m2/test/test1.html"));
    // // StringWriter stringWriter = new StringWriter();
    // Map<Object, Object> o = new HashMap<Object, Object>();
    // o.put("name", "logo");
    // o.put("ipItemList", list);
    // o.put("ipGroupList", list2);
    // o.put("serviceList", list3);
    // o.put("interfaceList", list7);
    // o.put("interfaceGroupList", list8);
    // o.put("ruleList", list10);
    // template.process(o, writer);
    // writer.close();
    // }
}
